#include "fig_spacerock.h"
#include "fig_player_craft.h"
#include "fig_enemybullet_master.h"
#include "fig_constants.h"

#include "num/rand.h"
#include "math/facing.h"


FIGSpaceRock::FIGSpaceRock(FIGEnemyCraft::ENEMY_TYPES EnemyType,
						   ROCK_TYPE RockType
						   ) :
											FIGEnemyCraft(EnemyType),
											m_RockType(RockType),
											m_Scale(1.0f)
{
	
	 m_Speed = 0.3f;
	 m_Accel = 2.0f;
	 m_Life = m_MaxLife = 5;
	 m_IsHitTestCircle = true;   
	 m_ExplodSfx = FIGSfx::EXPLODING_SPACEROCK;
	 m_DamageForColliding = FIGConstants::PLAYER_HP;
	m_SkipExplodeSingle = true;
	 
}




void	FIGSpaceRock::Render(const math::Vec2& Offset, float Scale)
{
	math::Vec2 TempOffset = Offset;
	if(m_RockType == ROCK_TYPE_DECAL)
		TempOffset = Offset * 0.25f;
	FIGEnemyCraft::Render(TempOffset, Scale * m_Scale);
}

void	FIGSpaceRock::ReSpawn()
{
	
	FIGEnemyCraft::ReSpawn();
	
	m_Pos = math::Vec2(num::RAND32::Instance().RandFloat(0.0f,1.0f), -m_Size.y);

	m_Speed = num::RAND32::Instance().RandFloat(0.1f,0.5f); 
	m_RotationSpeed = num::RAND32::Instance().RandFloat(5.0f,90.0f);
	if(num::RAND32::Instance().RandInt(0,1) == 0)
		m_RotationSpeed *= -1;

	m_Scale = num::RAND32::Instance().RandFloat(0.5f,1.0f);

	m_RotationSpeed *= m_Scale;

	m_IsHitAble = true;
	float V = 0.70f + m_Speed;
	if( V > 1.0f)
		V = 1.0f;
	m_Color = ColorfRGBA(V,V ,1.0f,1.0f);


	if(m_RockType == ROCK_TYPE_SLOW)
	{
		m_Speed *= 0.5f;
		m_RotationSpeed *= 0.5f;
	}
	else if(m_RockType == ROCK_TYPE_DECAL)
	{
		m_Scale *= 0.25f;
		float R =  num::RAND32::Instance().RandFloat(0.3f,0.4f);
		float G	=  num::RAND32::Instance().RandFloat(0.3f,0.4f);
		float B =  num::RAND32::Instance().RandFloat(0.3f,0.4f);		
		m_Color = ColorfRGBA(R,G,B,1.0f);
		m_RotationSpeed *= 0.1f;
		m_Speed *= 0.1f;
		m_IsHitAble = false;
	}


	if(m_Scale > 0.5f)
		m_ExplodeSize = 1 + (m_Scale * 0.15f);
	else
		m_ExplodeSize = 1.0f;
	
	m_Radius = m_Size.x * 0.5f * m_Scale;
	
}


void	FIGSpaceRock::Tick(float dt)
{
	
	
	FIGEnemyCraft::Tick(dt);

	if(!m_IsAlive)
		return;
	m_Rotation += (m_RotationSpeed * dt);
	m_Pos += ( math::Vec2(0,1.0f) *  m_Speed * dt);

	if(m_Pos.y > 1.0f + m_Size.y)
		m_IsAlive = false;

}

void	FIGSpaceRock::GotDamage(float Amount,FIGBullet *pBullet, bool Force)
{
	if( m_RockType == ROCK_TYPE_DECAL)
		return;
	FIGEnemyCraft::GotDamage(Amount,pBullet,Force);
}




